﻿using AutoMapper;
using YDT_ProductService.Application.DTOs;
using YDT_ProductService.Domains.Entities;

namespace YDT_ProductService.Application.Mappings
{
    /// <summary>
    /// AutoMapper映射配置 - 定义DTO和实体之间的映射规则
    /// </summary>
    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            // 商品映射配置
            CreateMap<Product, ProductDto>()
                .ForMember(dest => dest.ProductStatus,
                           opt => opt.MapFrom(src => src.ProductStatus.ToString()))
                .ForMember(dest => dest.ProductImages,
                           opt => opt.MapFrom(src => src.ProductImages));

            // 商品图片映射配置
            CreateMap<ProductImage, ProductImageDto>();

            // 创建商品映射配置
            CreateMap<CreateProductDto, Product>()
                .ForMember(dest => dest.ProductImages,
                           opt => opt.Ignore()); // 图片需要单独处理

            // 创建商品图片映射配置
            CreateMap<CreateProductImageDto, ProductImage>();

            // 更新商品映射配置
            CreateMap<UpdateProductDto, Product>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
        }
    }
}
